﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Main.Structure;
using Main.Substructure;

namespace Main.Menus.Submenus
{
    public partial class ManageAppointment : Form
    {
        private Appointment apptParam;
        public ManageAppointment(Appointment appt, bool modify)
        {
            InitializeComponent();

            Main(appt, modify);            
        }

        private void Main(Appointment appt, bool modify)
        {
            apptParam = appt;

            dateTimePicker.Value = new DateTime(appt.Ticks);
            dateTimePicker.MinDate = DateTime.Today;
            #region Hour Check
            var hour = 0;
            if (appt.Hour <= 12)
                hour = appt.Hour;
            else { hour = appt.Hour - 12; }
            #endregion
            numericHour.Value = hour                                                                                                                                          ;
            numericMinute.Value = appt.Minute;
            domainTOD.Text = appt.TimeOfDay;
            txtBoxNotes.Text = appt.Notes;

            #region Modify Boolean - TRUE
            if (modify)
            {
                dateTimePicker.Enabled = true;
                numericHour.Enabled = true;
                numericMinute.Enabled = true;
                domainTOD.Enabled = true;
                txtBoxNotes.Enabled = true;
                btnEdit.Visible = true;
                btnCancel.Visible = true;
            }
            #endregion
            #region Modify Boolean - FALSE
            else
            {
                dateTimePicker.Enabled = false;
                numericHour.Enabled = false;
                numericMinute.Enabled = false;
                domainTOD.Enabled = false;
                txtBoxNotes.Enabled = false;
                btnOK.Visible = true;
            }
            #endregion
        }

        /// <summary>
        /// This method is executed when the user clicks the "OK" button.
        /// This method disposes of the form.
        /// </summary>
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// This method is executed when the user clicks the "Cancel" button.
        /// This method disposes of the form.
        /// </summary>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// This method is executed when the user clicks the "Edit" button.
        /// This deletes the old appointment and saves the new one.
        /// </summary>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            var hour = numericHour.Value;
            // We gotta convert the time to military time and correct 12 o'clock times.
            #region Hour Check
            if (domainTOD.Text == "PM")
            {
                if (hour != 12)
                    hour += 12;
            }
            else
                if (hour == 12)
                    hour = 0;
            #endregion

            // Create a new DateTime variable uses the form data.
            DateTime DT = new DateTime(dateTimePicker.Value.Year, dateTimePicker.Value.Month, dateTimePicker.Value.Day,
                (int)hour, (int)numericMinute.Value, 0);
            // Create a new appointment by passing it that date's ticks and any notes the user may have entered.
            Appointment appt = new Appointment(DT.Ticks, txtBoxNotes.Text);

            if (Database.Appointments.Remove(apptParam) && Database.Appointments.Add(appt))
            {
                Utilities.Message("Appointment successfully edited!", "Appointment Modification", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Dispose();
            }
            else
                Utilities.Message("Appointment could not be modified.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
